class Checklist < ActiveRecord::Base

  ChecklistTypes = %w( before after )

  has_many :checklist_instances
  has_many :checklist_items
  has_many :checklist_recurrence_rules

  validates_presence_of :name
  
  validates_inclusion_of :checklist_type, :in => ChecklistTypes,
    :message => 'must be "after" or "before"'
  
  validate :has_items
  
  def new_checklist_item_attributes=(item_attributes)
    item_attributes.each { |attributes|
	  checklist_items.build(attributes)
	}
  end
  
  def couriers
    (checklist_instances.map { |x| x.courier } +
	 checklist_recurrence_rules.map {|x| x.courier }).uniq
  end
  
private

  def has_items
    errors.add(:tasks, "can not be empty") unless checklist_items.size > 0
  end

end
